<?php
defined('DCS_ROOT') or exit(header("HTTP/1.1 430 Not Forbidden"));

class main {
	function __construct (){
		$this->url = getUrl();
		$action = $this->url['action'];		
		$params = $this->url['params']['1'];
		if($params == 'new_review'){
			$this->addReview();
		}elseif($params == 'photos'){
			$this->showPhotos();
		}elseif($params == 'comments'){
			$this->showComments();
		}elseif($params == 'reviews'){
			$this->showReviews();
		}elseif($params == 'collects'){
			$this->showCollect();			
		}elseif(is_numeric($action)){
			$this->showMovie();
		}else{
			$this->defaultAction();
		}
		exit();
	}

	function defaultAction(){
		echo 'hi';
	}

	function showMovie(){
	//echo $_SESSION['last_key']['oauth_token'];
		require_once(APP_PATH."model/movie.php" );
		$db  = getDB();
		$data = new MovieModel($db);
		$id = $this->url['action'];		
		$movie = $data->getMovie($id);		
		//de($movie);
		empty($movie) && pageError();
		$tpl = new Template("movie/main.htm");	
		//$data->addView($id);

		$movie['rating'] = getRating($movie);
		$movie['rating_img'] = getStarImg($movie['rating']);
		$movie['playyear'] = date('Y',$movie['playtime']);

		//最新短评
		$comment = $data->getComments($id,'addtime',6);
		foreach($comment as $k => $v) {
			$comment[$k]['addtime'] = date('Y-m-d',$v['addtime']);
			$comment[$k]['stars'] = getStarImg($v['rating']);
			$comment[$k]['addtime'] = ''; // 新增的，隐藏时间
		}
		$tpl->assign('comment',$comment);
		
		
		
		
				
		//************************新增：取最新微博*********************
		$comment_new = $data->getComment_new($id,'created_at',6);   //从数据库取微博数据, 时间排序，取3个结果
		foreach($comment_new as $k => $v) {
			//$comment_new[$k]['addtime'] = $v['created_at'];
			//$comment_new[$k]['contents'] = $v['text'];
			//$comment_new[$k]['uid'] = $v['user_id'];
			//$comment_new[$k]['username'] = $v['user_name'];	
			$comment_new[$k]['stars'] = getStarImg($v['rating']);
		}
		
		//$tpl->assign('comment_new',$comment_new);     //指定到某个标识
		$i=0;
		foreach($comment as $item)   //把本地的内容存在0、2、4位置
		{
			$temp[$i]=$item;
		 $i+=2;
		}
		$i=1;
		foreach($comment_new as $item)    //把微博上的内容，存在1，3，5位置
		{
		 $temp[$i]=$item;
		 $i+=2;
		}
		for($i=0; $i<count($temp); $i++)  //为方便显示，重新排列数组
		{
		$temp2[$i] = $temp[$i];
		//$temp2[$i][mid]=$id;    // 把mid值放入数组，方便打印于页面，用js可以引用，然后可以用在转发、评论url
		}
		
		$j=0;
		for($i=0;$i<12;$i++)// 若本地数据很少，在数组会用空数据，利用一个循环，重新赋值一下数组，循环次数为6次
		{
		if(!empty($temp2[$i][id]))
			{
			$temp3[$j]=$temp2[$i];
			$j++;			
			}
		if($j==5) {break;}
		}
		//print_r($temp2);
		$tpl->assign('comment',$temp3);
		//************************新增：取最新微博*********************
		
		
		//************************新增：取“热门微博”*********************
		$comment_hot = $data->getComment_hot($id,'rt',5);   //从数据库取微博数据, 时间排序，取3个结果
		foreach($comment_hot as $k => $v) {
			$comment_hot[$k]['addtime'] = $v['created_at'];
			$comment_hot[$k]['contents'] = $v['text'];
			$comment_hot[$k]['uid'] = $v['user_id'];
			$comment_hot[$k]['username'] = $v['user_name'];	
			$comment[$k]['stars'] = getStarImg($v['rating']);
			$comment_hot[$k]['mid'] = $id;
			
		}
		$tpl->assign('comment_hot',$comment_hot);     //指定到某个标识
		//************************新增：取“热门微博”*********************
		
		
		
		

		//赞的短评
		$upcomment = $data->getComments($id,'addtime',5,'AND rating >= 5 ');
		foreach($upcomment as $k => $v) {
			$upcomment[$k]['addtime'] = date('Y-m-d',$v['addtime']);
			$upcomment[$k]['stars'] = getStarImg($v['rating']);
		}
		$tpl->assign('upComment',$upcomment);

		//弹的短评
		$downcomment = $data->getComments($id,'addtime',5,' AND rating < 5');
		foreach($downcomment as $k => $v) {
			$downcomment[$k]['addtime'] = date('Y-m-d',$v['addtime']);
			$downcomment[$k]['stars'] = getStarImg($v['rating']);
		}
		$tpl->assign('downComment',$downcomment);
		
		//热门影评
		$hot_reviews = $data->getReviews($id,'reply',6);
		foreach($hot_reviews as $k => $v) {
			$hot_reviews[$k]['addtime'] = date('Y-m-d',$v['addtime']);
			$hot_reviews[$k]['avatars'] = empty($v['avatars']) ? 'default.gif' : $v['avatars'];
			$hot_reviews[$k]['contents'] = csubstr(html2Txt($v['contents']),165);	
			$hot_reviews[$k]['stars'] = getStarImg($v['rating']);
		}
		$tpl->assign('hot_reviews',$hot_reviews);
		
		//最新影评
		$new_reviews = $data->getReviews($id,'addtime',5);
		foreach($new_reviews as $k => $v) {
			$new_reviews[$k]['addtime'] = date('Y-m-d',$v['addtime']);
			$new_reviews[$k]['avatars'] = empty($v['avatars']) ? 'default.gif' : $v['avatars'];
			$new_reviews[$k]['contents'] = csubstr(html2Txt($v['contents']),165);
			$new_reviews[$k]['stars'] = getStarImg($v['rating']);
		}
		$tpl->assign('new_reviews',$new_reviews);
		
		//图片列表
		$photos = $data->getPhotos($id,'addtime',5);
		$tpl->assign('photoList',$photos);
		
		$uid = getMember('uid');
		if($uid){
			$movie['myComment'] = $data->isPostComment($id,$uid);				
			if($movie['myComment']){
				$movie['myComment']['rating'] =  $movie['myComment']['rating'] / 2 ;
				
				$movie['myComment']['contents'] =  strip_tags($movie['myComment']['contents'])   ;	//新增：在编辑框显示自己微博前，去掉链接		
				
				if($movie['myComment']['type'] == 1 ){				
					$movie['myComment']['collected'] = ' checked="checked" ';
				}else{
					$movie['myComment']['isWish'] = ' $("#collectBox").hide(); ';
					$movie['myComment']['wished'] = ' checked="checked" ';
				}
			}
			//取 friend uid 列表
			$sql = "SELECT fid FROM member_friend WHERE uid = '$uid' LIMIT 0,8 ";
			$db->query($sql);
			$fid = $db->fetchAll();		
			foreach($fid as $k => $v){
				$fids .= "'".$v['fid']."',";
			}
			$fids = substr($fids,0,-1);
			
			//如果有朋友
			if($fids){
				//朋友看过
				$friendCollect = $data->getFriendCollect($id,$fids,10,2);
				$tpl->assign('friendCollect',$friendCollect);
				//朋友想看
				$friendWish = $data->getFriendCollect($id,$fids,10,1);
				$tpl->assign('friendWish',$friendWish);				
				//朋友短评
				$friendComments = $data->getFriendComments($id,$fids,'c.addtime',5);
				if(!empty($friendComments)){
					foreach($friendComments as $k => $v) {
						$friendComments[$k]['addtime'] = date('Y-m-d',$v['addtime']);
						$friendComments[$k]['stars'] = getStarImg($v['rating']);
						$friendComments[$k]['mid'] = $id;  			//新增：电影id信息
					}
					$tpl->assign('friendComments',$friendComments);
				}else{
					$movie['FCTip'] = '你的朋友中，还没有人评论过该电影。';
				}				
			}else{
				$movie['FCTip'] = '你还没有朋友，多去认识些朋友吧。';
			}
		}else{
			$movie['FCTip'] = '你还未登录，请先登录后再来看看吧。';
		}
		
		$movie['ftimes'] = time();
		$tpl->assign($movie);
		$tpl->assign(assignUser());
		$tpl->assign("debuginfo",debuginfo().$db->getQueryTimes().' 次查询');
		$tpl->output();
	}

	function showComments(){ // 这部分是显示 短评列表
		require_once(APP_PATH."model/movie.php" );
		$db  = getDB();
		$data = new MovieModel($db);
		$id = $this->url['action'];		
		$movie = $data->getMovie($id);
		//de($movie);
		empty($movie) && pageError();
		$myuid = getMember('uid');

		$tpl = new Template("movie/comments.htm");
		
		if($this->url['params']['2'] == 'friend'){   //这里是判断取“朋友”的微博
			$pagetemp = $this->url['params']['3'];
			if($myuid){
				//取 friend uid 列表
				$sql = "SELECT fid FROM member_friend WHERE uid = '$myuid' ";
				$db->query($sql);
				$fid = $db->fetchAll();		
				foreach($fid as $k => $v){
					$fids .= "'".$v['fid']."',";
				}
				$fids = substr($fids,0,-1);

				if($fids){
					$param['table'] = ' movie_comments AS c, member AS m  ';
					$param['field'] = ' c.id,c.uid,c.rating,c.contents,c.support,c.addtime,m.username,m.avatars,c.rt,c.comments,c.weibo_id,c.profile_image_url,c.mid '; //
					$param['where'] = " c.uid IN($fids) AND c.uid = m.uid AND c.mid = '$id' AND c.contents != '' ";
					$param['order'] = ' c.addtime DESC';
					$param['pageUrl'] = '/movie/'.$id.'/comments/friend/';
					$param['page']= is_numeric($pagetemp) ? $pagetemp :'1';
					$param['rows'] = '16';
					$param['pageTotal']	= $db->getTotal($param);
					$comments = $db->getList($param);
					if(empty($comments)){
						$movie['friendCommentTip'] = '你的朋友中，还没有人评论过该电影。';
					}
				}else{
					$movie['friendCommentTip'] = '你还没有朋友，多去认识些朋友吧。';
				}
			}else{
				$movie['friendCommentTip'] = '您还未登录，登录后才能看到您朋友的短评。 ';
			}
			$movie['friendCommentTab'] = 'active';
		}	
		
			
			
		//××××××××××××××××新增在comment list取“热门”微博×××××××××××××××××××××××××××××××××××××
		elseif($this->url['params']['2'] == 'hot'){   //这里是判断取“朋友”的微博
			$pagetemp = $this->url['params']['3'];
			$param['table'] = ' comment_hot AS c ';
			$param['field'] = ' c.id,c.user_id as uid,c.rating, c.text as contents,c.support,unix_timestamp(c.created_at) as addtime,c.profile_image_url, c.user_name as username, c.rt, c.comments, c.weibo_id,c.movie_id as mid ';		
			$param['where'] = " c.movie_id = '$id'  ";
			$param['order'] = ' c.rt DESC';
			$param['pageUrl'] = '/movie/'.$id.'/comments/hot/';
			$param['page']= is_numeric($pagetemp) ? $pagetemp :'1';
			$param['rows'] = '16';
			$param['pageTotal']	= $db->getTotal($param);
			$comments = $db->getList($param);
			$movie['hotCommentTab'] = 'active';
		}
		//××××××××××××××××新增在comment list取“热门”微博×××××××××××××××××××××××××××××××××××××		
		
		
			
		elseif($this->url['params']['2'] == '' OR $this->url['params']['2'] == 'page'){    //这里默认显示“最新”微博
			$pagetemp = $this->url['params']['3'];
			$param['table'] = ' movie_comments AS c, member AS m ';
			$param['field'] = ' c.id,c.uid,c.rating,c.contents,c.support,c.addtime,m.username,m.avatars,c.profile_image_url,c.rt,c.comments, c.mid,c.weibo_id '; //增加获取微博头像,转发，评论
			$param['where'] = " c.mid = '$id' AND c.uid = m.uid AND c.contents != '' ";
			$param['order'] = ' c.addtime DESC';
			$param['pageUrl'] = '/movie/'.$id.'/comments/page/';
			$param['page']= is_numeric($pagetemp) ? $pagetemp :'1';
			$param['rows'] = '12';
			$param['pageTotal']	= $db->getTotal($param);
			$comments = $db->getList($param);
			$movie['newCommentTab'] = 'active';
			
			
			
// ××××××××××××××××××这里插入 comment_new的数据，交叉放入到list 页面×××××××××××××××××××××××××××××××
			$param['table'] = ' comment_new AS c ';
			$param['field'] = ' c.id,c.user_id,c.rating,c.text as contents,c.support, unix_timestamp(c.created_at) as addtime,c.user_name as username,c.profile_image_url,c.rt,c.comments,c.weibo_id,c.movie_id as mid '; //增加获取微博头像,转发，评论
			$param['where'] = " c.movie_id = '$id'  ";
			$param['order'] = ' c.created_at DESC';
			$param['pageUrl'] = '/movie/'.$id.'/comments/page/';
			$param['page']= is_numeric($pagetemp) ? $pagetemp :'1';
			$param['rows'] = '16';
			$param['pageTotal']	= $db->getTotal($param);
			$comment_new = $db->getList($param);
			
	     	$i=0;
			foreach($comments as $item)   //把本地的内容存在0、2、4位置
			{
				$temp[$i]=$item;
			 $i+=2;
			}
			$i=1;
			foreach($comment_new as $item)    //把微博上的内容，存在1，3，5位置
			{
			 $temp[$i]=$item;
			 $i+=2;
			}
			for($i=0; $i<count($temp); $i++)  //为方便显示，重新排列数组
			{
			$temp2[$i] = $temp[$i];
			//$temp2[$i][mid]=$id;    // 把mid值放入数组，方便打印于页面，用js可以引用，然后可以用在转发、评论url
			}
			
			$j=0;
			for($i=0;$i<2000;$i++)// 若本地数据很少，在数组会用空数据，利用一个循环，重新赋值一下数组，循环次数为6次
			{
			if(!empty($temp2[$i][id]))
				{
				$temp3[$j]=$temp2[$i];
				$j++;			
				}
			if($j==1500) {break;}
			}
           $comments=$temp3;
// ××××××××××××××××××这里插入 comment_new的数据，交叉放入到list 页面×××××××××××××××××××××××××××××××



		}		
	  		if(!empty($comments)){
			foreach($comments as $k => $v ){
				$comments[$k]['addtime'] = date("Y-m-d",$v['addtime']);
				$comments[$k]['stars'] = getStarImg($v['rating']);
			}		
			$tpl->assign('commentList',$comments);
			$tpl->assign("pages", makePage($param));
	 }  //以上是部分是取“最新”的微博

		if($myuid){
			$movie['myComment'] = $data->isPostComment($id,$myuid);
			if($movie['myComment']){
				$movie['myComment']['rating'] =  $movie['myComment']['rating'] / 2 ;	

				$movie['myComment']['contents'] =  strip_tags($movie['myComment']['contents'])   ;	//新增：在编辑框显示自己微博前，去掉链接
				
				if($movie['myComment']['type'] == 1 ){				
					$movie['myComment']['collected'] = ' checked="checked" ';
				}else{
					$movie['myComment']['isWish'] = ' $("#collectBox").hide(); ';
					$movie['myComment']['wished'] = ' checked="checked" ';
				}
			}
		}

		$movie['total'] = $param['pageTotal'];
		$tpl->assign($movie);
		$tpl->assign(assignUser());		
		$tpl->assign("debuginfo",debuginfo().$db->getQueryTimes().' 次查询');
		$tpl->output();
	}

	function showReviews(){
		require_once(APP_PATH."model/comments.php" );
		$db  = getDB();
		$data = new CommentModel($db);
		$id = $this->url['action'];		
		$movie = $data->getMovie($id);
		//de($movie);
		empty($movie) && pageError();
		$tpl = new Template("movie/reviews.htm");

		if($this->url['params']['2'] == 'hot'){
			$pagetemp = $this->url['params']['3'];
			$param['order'] = ' r.reply DESC,r.support DESC ';
			$param['pageUrl'] = '/movie/'.$id.'/reviews/hot/';
			$movie['hotTab'] = ' active ';
		}elseif($this->url['params']['2'] == 'new'){
			$pagetemp = $this->url['params']['3'];
			$param['order'] = ' r.addtime DESC ';
			$param['pageUrl'] = '/movie/'.$id.'/reviews/new/';
			$movie['newTab'] = ' active ';
		}elseif($this->url['params']['2'] == ''){
			$param['order'] = ' r.reply DESC,r.support DESC ';
			$param['pageUrl'] = '/movie/'.$id.'/reviews/hot/';
			$movie['hotTab'] = ' active ';
		}
		
		//逻辑热门：然后按“有用+回应”数进行排序
		$param['table'] = ' movie_review AS r,member AS m ';
		$param['field'] = ' r.id,r.uid,r.mid,r.rating,r.title,r.contents,r.reply,r.support,r.addtime,m.username,m.avatars,m.profile_image_url ';
		$param['where'] = " r.uid = m.uid AND r.mid = '$id' ";		
		$param['page']= is_numeric($pagetemp) ? $pagetemp :'1';		
		$param['rows'] = '10';		
		$param['pageTotal']	= $db->getTotal($param);		
		$reviews = $db->getList($param);		
		foreach($reviews as $k => $v ){
			$reviews[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
			$reviews[$k]['avatars'] = empty($v['avatars']) ? 'default.gif' : $v['avatars'];
			$reviews[$k]['contents'] = csubstr(html2Txt($v['contents']),120);
			$reviews[$k]['stars'] = getStarImg($v['rating']);
		}
		//de($comments);
		$tpl->assign('reviewList',$reviews);

		$tpl->assign("pages", makePage($param));
		$movie['total'] = $param['pageTotal'];
		$tpl->assign($movie);
		$tpl->assign(assignUser());
		$tpl->assign("debuginfo",debuginfo().$db->getQueryTimes().' 次查询');
		$tpl->output();
	}

	function showPhotos(){
		require_once(APP_PATH."model/movie.php" );
		$db  = getDB();
		$data = new MovieModel($db);
		$id = $this->url['action'];		
		$movie = $data->getMovie($id);
		$movie['playyear'] = date('Y',$movie['playtime']);
		//de($movie);
		empty($movie) && pageError();
		$tpl = new Template("movie/photos.htm");	
		if($this->url['params']['2'] == 'page'){
			$pagetemp = $this->url['params']['3'];		
		}
		$param['table'] = 'movie_photo ';
		$param['field'] = ' id,filename ';
		$param['where'] = " mid = '$id' ";
		$param['order'] = 'addtime DESC ';
		$param['page']= is_numeric($pagetemp) ? $pagetemp :'1';		
		$param['rows'] = '20';
		$param['pageUrl'] = '/movie/'.$id.'/photos/page/';
		$param['pageTotal']	= $db->getTotal($param);
		$photos = $db->getList($param);
		$i = 0;
		foreach($photos as $k => $v) {			
			if($i >= 4){
				$photos[$k]['islast'] = ' class="last"';
				$i = 0;
			}
			$i ++;
		}
		$tpl->assign('photoList',$photos);		
		$new_reply = $data->getPhotoReply($id,'addtime',50);
		foreach($new_reply as $k => $v ){
			$new_reply[$k]['addtime'] = date("m-d H:i",$v['addtime']);
			$new_reply[$k]['avatars'] = empty($v['avatars']) ? 'default.gif' : $v['avatars'];
		}
		$tpl->assign('new_reply',$new_reply);		

		$tpl->assign($movie);
		$tpl->assign(assignUser());
		$tpl->assign("pages", makePage($param));
		$tpl->assign("debuginfo",debuginfo().$db->getQueryTimes().' 次查询');
		$tpl->output();
	}

	function addReview(){
		require_once(APP_PATH."model/movie.php" );
		$db  = getDB();
		$data = new MovieModel($db);
		$id = $this->url['action'];		
		$movie = $data->getMovie($id);
		//de($movie);
		empty($movie) && pageError();
		$tpl = new Template("movie/main.htm");		
		$article['addtime'] = date('Y年m月d日 H:i',$article['addtime']);		
		$data->addView($id);	
		$tpl->assign(assignUser());
		$tpl->assign($movie);
		$tpl->assign("debuginfo",debuginfo().$db->getQueryTimes().' 次查询');
		$tpl->output();
	}

	function showCollect(){	
		$tpl = new Template("movie/collects.htm");	
		$tpl->assign($movie);
		$tpl->output();
	}

}
?>
